<?php
namespace app\index\controller;
use think\Db;
header('Access-Control-Allow-Origin:*');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept"); 
class Evaluate extends Base
{
	//评价插入
	public function insert(){

		if(empty($_POST['evaluate'])){
			echo json_encode(array('status'=>1,'info'=>'缺少参数'));
			exit;
		}
		$data=$_POST['evaluate'];
		$data['create_time']=time();
		$data['update_time']=time();
		$result = $this->validate($data,'Evaluate');
		if(true !== $result){
		    $res =array(
		    	'status' => 1,
		    	'info'  => $result
		    );
		    echo json_encode($res);
		    exit;
		}
		$evaluate=model('evaluate');
		$order=model('order');
		$food=model('food');
		$res=$evaluate->deny_insert($data['uid'],$data['fid'],$data['create_time']);
		if($res){
			echo json_encode(array('status'=>1,'info'=>'你已经提交过评价！'));
			exit;
		}
		$response=$evaluate->insert($data);
		if($response){
			$fooddata=$food->field('assess,assess_count')->where(array('id'=>$data['fid']))->find();
			$res_2=$food->save(['assess'=>intval($fooddata['assess'])+intval($data['assess']),'assess_count'=>intval($fooddata['assess_count'])+1],['id'=>$data['fid']]);
			$res=array(
				'status' =>0
			);
		}else{
			$res=array(
				'status' =>1,
				'info'=>'提交失败'
			);
		}
		echo json_encode($res);
	}
	public function list(){
		if(empty($_GET['fid'])){
			echo json_encode(array('status'=>1,'info'=>'缺少参数'));
			exit;
		}
		$fid=$_GET['fid'];
		$evaluate=model('evaluate');
		$data=$evaluate->field('u.photo,e.content,e.img_url,e.create_time,u.username')->alias('e')->join('td_homeuser u','e.uid = u.id')->where(array('e.fid'=>$fid))->select();
		if($data){
			$res=array(
				'status' => 0,
				'info' =>$data
			);
		}else{
			$res=array(
				'status' =>1
			);
		}
		echo json_encode($res);
	}
}
